#include <stdio.h>
#include <stdlib.h>


int array_size, *array_list ;

void array_append()
        {
        int x ;
        printf("Enter number to append");
        scanf("%d",&x);
        array_size++;
        array_list=realloc(array_list,(array_size+1)* sizeof(*array_list));
        array_list[array_size]=x;
        }
void array_extend()
        {
        int i, *t, s;
        printf("\nEnter no. of elements you would like to Extend in the List");
        scanf("%d",&s);
        t=malloc((s)*sizeof(*t));
        printf("\nEnter the inputs: ");
        for(i=0;i<s;i++)
        {
         scanf("%d ",&t[i]);
        }
        array_list=realloc(array_list,(array_size+s+1)*sizeof(*array_list));
        for(i=0;i<s;i++)
        {
         array_size++;
         array_list[array_size]=t[i];
        }
        }
void array_insert()
        {
        int i,x, index;
        printf("Enter Position");
        scanf(" %d",&index);
        if(index<=array_size+1)
                {
                printf("Enter the number");
                scanf("%d",&x);
                array_list=realloc(array_list,(array_size+1)*sizeof(*array_list));
                for(i=array_size;i>=(index-1);i--)
                {
                        array_list[i+1]=array_list[i];
                }
                array_list[index-1]=x;
                array_size++;
                }
        else
                printf("Invalid Position!!!");
        }

void array_display()
        {
        int i;
        printf("\nThe Current List is [");
        for(i=0;i<=array_size;i++)
                {
                printf("%d ",array_list[i]);
                }
        printf("]\n");
        }
void array_remove()
        {
        int i,flag,x;
        flag = 0 ;
        printf("\nEnter number to be removed");
        scanf("%d",&x);
        for(i=0;i<=array_size;i++)
                {
                if(array_list[i]==x)
                        {
                        flag = 1 ;
                        while(i<array_size)
                                {
                                array_list[i]=array_list[i+1];
                                i++;
                                }
                        array_list=realloc(array_list,(array_size)*sizeof(*array_list));
                        array_size--;
                        break;
                        }
                }
        if(flag==0)
                printf("\nNumber Not Found!!!") ;
        }

void array_indexremove()
        {
        int i,x,index;
        printf("Enter index to be removed(0-%d):", array_size);
        scanf("%d",&index);
        if(index<=array_size && index>=0)
                {
                x=array_list[index];
                for(i=index;i<array_size;i++)
                {
                        array_list[i]=array_list[i+1];
                }
                array_size--;
                array_list=realloc(array_list,array_size*sizeof(*array_list));
                printf("\nThe Element %d, Removed!",x);
                }
        else
                printf("\nInvalid Index!!!");
        
        
        }

void array_index()
        {
        int index ;
        printf("\nEnter Index(0-%d):", array_size);
        scanf("%d",&index);
        if(index<=array_size)
                {
                printf("\nElement at position %d is %d",index,array_list[index]);
                }
        else
                {
                printf("\nInvalid Index!!!");
                }
        }

void array_sort()
        {
        int i,j;
        for(i=0;i<=array_size-1;i++)
                {
                for(j=i+1;j<=array_size;j++)
                        {
                        if(array_list[i]>array_list[j])
                                {
                                 array_list[i]=array_list[i]+array_list[j];
                                 array_list[j]=array_list[i]-array_list[j];
                                 array_list[i]=array_list[i]-array_list[j];
                                }
                        }
                }
        }


void array_reverse()
        {
        int i,j;
        for(i=0,j=array_size;i<j;i++,j--)
                {
                array_list[i]=array_list[i]+array_list[j];
                array_list[j]=array_list[i]-array_list[j];
                array_list[i]=array_list[i]-array_list[j];
                }
        }

void array_counter()
        {
        int i,ctr,x;
        ctr = 0 ;
        printf("\nEnter Integer to be counted: ");
        scanf("%d",&x);
        for(i=0;i<=array_size;i++)
                {
                if(array_list[i]==x)
                        ctr++;
                }
        printf("\nThe integer %d appears in the array %d times.", x, ctr) ;
        }

void main()
        {
        int choice, i;
        char c ;
        printf("\nList Initialization:");
        printf("\nEnter no. of elements: ");
        scanf("%d",&array_size);
        array_size = array_size-1;
        array_list=malloc((array_size+1)* sizeof(*array_list));
        printf("\nEnter the inputs: ");
        for(i=0;i<=array_size;i++)
                {
                scanf(" %d",&array_list[i]);
                }
        choice=1 ;
        do
                {
                printf("\n\t Dynamic Array List Menu\n\t=========================") ;
                printf("\n 1) Append\n 2) Extend\n 3) Insert\n 4) Remove\n 5) Pop\n 6) Index\n 7) Count\n 8) Sort\n 9) Reverse");
                printf("\n Enter Your Choice(0 to Exit): ") ;
                scanf(" %d",&choice);
                switch(choice)
                        {
                        case 1: array_append();
                                break;
                        case 2: array_extend();
                                break;
                        case 3: array_insert();
                                break;
                        case 4: array_remove();
                                break;
                        case 5: array_indexremove();
                                break;
                        case 6: array_index();
                                break;
                        case 7: array_counter();
                                break;
                        case 8: array_sort();
                                break;
                        case 9: array_reverse();
                                break;
                        default: break;
                        }
                array_display();
                if(choice!=0)
                {
                printf("\nInput any character to continue ") ;
                scanf(" %s",&c);
                }
              }while(choice);
        }       
